package com.xuecheng.manage_cms.service;

import com.xuecheng.framework.domain.cms.CmsSite;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_cms.dao.CmsSiteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 站点管理实现层
 */
@Service
public class CmsSiteService {
    
    
    @Autowired private CmsSiteRepository cmsSiteRepository;

    /**
     * 站点分页查询接口
     * @param page
     * @param size
     * @param cmsSite
     * @return
     */
    public QueryResponseResult findList(int page, int size, CmsSite cmsSite){
        //条件匹配器,
        ExampleMatcher exampleMatcher =ExampleMatcher.matching();
        //创建模糊条件查询
        exampleMatcher =exampleMatcher.withMatcher("siteName",ExampleMatcher.GenericPropertyMatchers.contains());
        //创建条件实例，有意思的是实体类字段有值的时候自动默认是精确查询，所以有时候可能要过滤一下
        Example<CmsSite> example= Example.of(cmsSite,exampleMatcher);
        Pageable pageable= PageRequest.of(page,size);

        Page<CmsSite> list= cmsSiteRepository.findAll(example,pageable);
        QueryResult<CmsSite> queryResult=new QueryResult<CmsSite>();
        queryResult.setList(list.getContent());
        queryResult.setTotal(list.getTotalElements());
        queryResult.setTotalPage(list.getTotalPages());
        return new QueryResponseResult(CommonCode.SUCCESS,queryResult);

    }

    /**
     * 查询所有
     * @return
     */
    public ResponseResult<List<CmsSite>> findAllList(){
        List<CmsSite> list= cmsSiteRepository.findAll();
        return new ResponseResult(CommonCode.SUCCESS,list);
    }

    /**
     * 站点新增接口
     * @param cmsSite
     * @return
     */
    public ResponseResult<CmsSite> add(CmsSite cmsSite){
        //先查询有没有此站点
        CmsSite data=cmsSiteRepository.findBySiteNameAndSiteWebPath(cmsSite.getSiteName(),cmsSite.getSiteWebPath());
        if(data==null){
            cmsSite.setSiteId(null);
            cmsSite.setSiteCreateTime(new Date());
            cmsSiteRepository.save(cmsSite);
            return ResponseResult.SUCCESS(cmsSite);
        }
        return new ResponseResult(CommonCode.FAIL,cmsSite);
    }


    /**
     * 根据id查询站点
     * @param id
     * @return
     */
    public ResponseResult<CmsSite> findById(String id){
        Optional<CmsSite> optional = cmsSiteRepository.findById(id);
        return new ResponseResult(CommonCode.SUCCESS,optional.get());
    }

    /**
     * 更新站点信息
     * @param cmsSite
     * @return
     */
    public ResponseResult<CmsSite> update(CmsSite cmsSite) {
        //根据id查询站点信息
        CmsSite one = cmsSiteRepository.findById(cmsSite.getSiteId()).get();
            if (one != null) {
                cmsSite.setSiteCreateTime(one.getSiteCreateTime());
                //执行更新
                CmsSite save = cmsSiteRepository.save(cmsSite);
                if (save != null) {
                    //返回成功
                    return ResponseResult.SUCCESS(save);
                }
            }
        //返回失败
        return new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 站点删除接口
     * @param id
     * @return
     */
    public ResponseResult delete(String  id){
        if(id!=null){
            cmsSiteRepository.deleteById(id);
            return  new ResponseResult(CommonCode.SUCCESS);
        }
        return  new ResponseResult(CommonCode.FAIL);
    }
}
